<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+


$file_conf['extract_save_dir'] = APP_CACHE_DIR;

require_once 'eleontev/Dir/MyDir.php';
require_once 'eleontev/Dir/mime_content_type.php';


$controller->loadClass('FileCategoryModel', 'file/category');
$controller->loadClass('RoleModel', 'role');

$controller->loadClass('FileEntry', 'file/entry');
$controller->loadClass('FileEntryModel', 'file/entry');
$controller->loadClass('FileEntryModel_dir', 'file/entry');
$controller->loadClass('FileEntryView_common', 'file/entry');
//$controller->loadClass('FileEntryModel_db');

$controller->loadClass('BulkFileEntry');

// initialize objects
$rq =& new RequestData($_GET);
$rp =& new RequestData($_POST);

// settings
$setting = &new SettingModel();
$setting->setModuleId('file');
$setting =& $setting->getSettings();
$setting = array_merge($setting, $file_conf);

$obj = new BulkFileEntry;

$manager =& $obj->setManager( new FileEntryModel_dir );
$manager->setFileSetting($setting);

$manager->checkPriv($priv, $controller->action, @$rq->id);


switch ($controller->action) {
case 'delete': // ------------------------------
	
	
	$controller->go();

	break;
	

case 'insert': // ------------------------------
	
	
	$controller->go();

	break;
	
	
default: // ------------------------------------
	
	$files = array();
	$next = false;
	
	if(isset($rp->submit)) {
			
		$is_error = &$obj->validate($rp->vars, $rp->step);
		
		if($is_error) {
			$rp->stripVars(true);
			
			$obj->set($rp->vars);
			$obj->setCategory(@$rp->vars['category']);
			
			if(!empty($rp->vars['category_mirror'])) {
				$obj->setCategoryMirror($rp->vars['category_mirror']);
			}
			
			if(!empty($rp->vars['member_role'])) {
				$obj->setMemberRole($rp->vars['member_role']);
			}
		
		} else {
		
			set_time_limit(0);
			ignore_user_abort();
			
			//$rp->stripVars('striplashes');
			$rp->stripVars();
			
			$obj->set($rp->vars);
			$obj->setCategory(@$rp->vars['category']);

			if(!empty($rp->vars['category_mirror'])) {
				$obj->setCategoryMirror($rp->vars['category_mirror']);
			}
			
			if(!empty($rp->vars['member_role'])) {
				$obj->setMemberRole($rp->vars['member_role']);
			}
			
			
			foreach($rp->files as $k => $file) {
				
				$upload = $manager->getFileData($file);
				$content = &$manager->getFileContent($upload['to_read']);
				
				if($content) {

					$file_id = $manager->saveFileData($content);
					
					$obj->set('id', $file_id);
					$obj->set('filename', addslashes($upload['name']));
					$obj->set('directory', addslashes($upload['directory']));
					$obj->set('filesize', $upload['size']);
					$obj->set('filetype', $upload['type']);
					
					if($manager->isExtra('file')) {
						if($manager->setting['file_extract']) {
							
							$controller->loadClass('FileTextExctractor');
							
							$extractor = &new FileTextExctractor($upload['extension']);
							$extractor->setTool($setting['extract_tool']);
							$extractor->setExtractDir($setting['extract_save_dir']);
							
							$obj->set('filetext', addslashes($extractor->getText($upload['to_read'])));
						}
					}
					
					$manager->save($obj, 'insert', true);
				}
			}
			
			$return = $controller->getLink('file', 'file_entry');
			$controller->setCustomPageToReturn($return, false);
			$controller->go();			
		}
	}
	
	//if($next) {
		$view = &$controller->getView($obj, $manager, 'BulkFileEntryView_two', $files);
	//} else {
	//	$view = &$controller->getView($obj, $manager, 'BulkFileEntryView_one');
	//}
}
?>